//
//  SISSMortgageLoansViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/9/1.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSMortgageLoansViewController.h"
#import "SISSProgressWebViewController.h"
#import "SISSMortgageLoansBankShowViewController.h"

static NSString *normalCellReuseID = @"normalCellReuseID";
@interface SISSMortgageLoansViewController ()<UITableViewDataSource,UITableViewDelegate>

// banksDataSource
@property (strong, nonatomic) NSArray *banksDataSource;

@end

@implementation SISSMortgageLoansViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"按揭贷款";
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:normalCellReuseID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:normalCellReuseID];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [cell.contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.equalTo(cell.contentView);
            make.right.equalTo(cell.contentView).offset(32);
            make.height.mas_equalTo(1);
        }];
    }
    if (indexPath.section == 0) {
        NSArray *arr = @[@"商业贷款", @"公积金贷款", @"组合贷款"];
        cell.textLabel.text = arr[indexPath.row];
    } else {
        cell.textLabel.text = self.dataArr[indexPath.section][indexPath.row];
    }
    
    cell.textLabel.textColor = MAIN_TEXT_COLOR;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        
        SISSProgressWebViewController *vc = [[SISSProgressWebViewController alloc] init];
        NSArray *arr = @[@"商业贷款", @"公积金贷款", @"组合贷款"];
        vc.title = arr[indexPath.row];
        vc.url = self.dataArr[indexPath.section][indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        SISSMortgageLoansBankShowViewController *vc = [[SISSMortgageLoansBankShowViewController alloc] init];
        vc.title = self.dataArr[indexPath.section][indexPath.row];
        vc.dataSource = self.banksDataSource[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    if (section > 0) {
        view.backgroundColor = [UIColor groupTableViewBackgroundColor];
        UILabel *label = [UILabel yyr_labelWithText: @"二手房贷款银行选择" fontSize:17 textColor:MAIN_TEXT_COLOR];
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view).offset(16);
            make.centerY.equalTo(view);
        }];
    }
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return section > 0 ? 44 : 0;
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    SISSBaseTableView *tableView = [[SISSBaseTableView alloc] initWithFrame:self.view.bounds];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:normalCellReuseID];
    [self.view addSubview:tableView];
}

- (NSArray *)banksDataSource {
    if (!_banksDataSource) {
        //获取plist
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"MortgageLoansBanksArray" withExtension:@"plist"];
        
        //根据plist加载数组
        _banksDataSource =  [NSArray arrayWithContentsOfURL:url];;
    }
    return _banksDataSource;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
